package com.woniuxy.carlife.carlifenewcomplaintsweb.controller;


import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.carlifecommonsweb.util.JwtUtil;
import com.woniuxy.carlife.carlifenewcomplaintsdomain.dto.UserComplaintDto;
import com.woniuxy.carlife.carlifenewcomplaintsdomain.param.UserComplaintParam;
import com.woniuxy.carlife.carlifenewcomplaintsweb.service.UserComplaintService;
import com.woniuxy.carlife.carlifenewcomplaintsweb.utils.MinioTemplate;
import com.woniuxy.carlife.carlifenewcomplaintsweb.utils.StatusMessageEnum;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author yp
 * @ClassName:UserComplaintController
 * @Description:用户投诉
 * @date 2021/4/7 2:32
 **/
@RestController
@Slf4j
@Api(tags = "用户投诉管理接口")
@RequestMapping("/userComplaint")
public class UserComplaintController {

    @Resource
    private UserComplaintService userComplaintService;


    @Resource
    private MinioTemplate minioTemplate;

    /*
     * 功能描述〈添加投诉〉
     * @author:yp
     * @param:UserComplaintParam
     * @return:
     * @Date 2021/4/12 15:16
     * @throws
     */
    @PostMapping("/ordinary/addUserComplaint")
    @ApiOperation(value = "添加用户投诉信息",notes = "<span style='color:red;'>添加用户投诉信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "添加成功"),
            @ApiResponse(code=20001,message = "添加失败")
    })
    public Result addUserComplaint(@RequestBody @ApiParam UserComplaintParam userComplaintParam){
        if(userComplaintParam==null){
            return new Result(false,StatusCode.ERROR,"参数错误");
        }
        ServletRequestAttributes sra =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        userComplaintParam.setUserId(tokenUserId);
        log.info("开始业务:添加投诉");
        int num = userComplaintService.addUserComplaint(userComplaintParam);
        log.info("结束业务:添加投诉");
        if (num==1){
            return new Result(true, StatusCode.OK, StatusMessageEnum.SAVE_SUCCESS.toString());
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.SAVE_FAIL.toString());
    }

    /*
     * 功能描述〈查询自己的投诉〉
     * @author:yp
     * @param:UserComplaintParam
     * @return:List<UserComplaintDto>
     * @Date 2021/4/12 15:17
     * @throws
     */
    @GetMapping("/ordinary/findUserComplaint")
    @ApiOperation(value = "用户查看自己的投诉信息",notes = "<span style='color:red;'>查询用户的投诉信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功"),
            @ApiResponse(code=20001,message = "查询失败")
    })
    public Result findUserComplaint(){
        ServletRequestAttributes sra =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        UserComplaintParam userComplaintParam=new UserComplaintParam();
        userComplaintParam.setUserId(tokenUserId);
        log.info("开始业务:查询投诉");
        List<UserComplaintDto> list = userComplaintService.findUserComplaint(userComplaintParam);
        log.info("结束业务:查询投诉");
        if (list.size()!=0){
            return new Result(true,StatusCode.OK,StatusMessageEnum.FIND_SUCCESS.toString(),list);
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.FIND_FAIL.toString());
    }

    /*
     * 功能描述〈门店查询投诉〉
     * @author:yp
     * @param:UserComplaintParam
     * @return:List<UserComplaintDto>
     * @Date 2021/4/12 15:17
     * @throws
     */
    @GetMapping("/merchantmanage/findComplaint")
    @ApiOperation(value = "门店查询投诉信息",notes = "<span style='color:red;'>门店查询投诉信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功"),
            @ApiResponse(code=20001,message = "查询失败")
    })
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "storeId",value = "门店id",dataType = "int",paramType = "query",example = "1"),
    })
    public Result findComplaint(Integer storeId){
        if(storeId==null){
            return new Result(false,StatusCode.ERROR,"参数错误");
        }
        log.info("开始业务:查询投诉");
        List<UserComplaintDto> list = userComplaintService.findComplaint(storeId);
        log.info("结束业务:查询投诉");
        if (list.size()!=0){
            return new Result(true,StatusCode.OK,StatusMessageEnum.FIND_SUCCESS.toString(),list);
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.FIND_FAIL.toString());
    }


    /*
     * 功能描述〈修改投诉〉
     * @author:yp
     * @param: UserComplaintParam
     * @return:
     * @Date 2021/4/12 15:19
     * @throws
     */
    @PostMapping("/ordinary/updateUserComplaint")
    @ApiOperation(value = "用户修改投诉信息",notes = "<span style='color:red;'>用户修改投诉信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "修改成功"),
            @ApiResponse(code=20001,message = "修改失败")
    })
    public Result updateUserComplaint(@ApiParam @RequestBody UserComplaintParam userComplaintParam){
        if(userComplaintParam==null){
            return new Result(false,StatusCode.ERROR,"参数错误");
        }
        ServletRequestAttributes sra =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        JwtUtil jwtUtil = new JwtUtil();
        Claims claims = jwtUtil.parseTokenOrdinary(token);
        Object o = claims.get("token");
        Integer tokenUserId = Integer.valueOf(String.valueOf(o));
        userComplaintParam.setUserId(tokenUserId);
        log.info("开始业务:修改投诉");
        int num =userComplaintService.updateUserComplaint(userComplaintParam);
        log.info("结束业务:修改投诉");
        if (num==1){
            return new Result(true,StatusCode.OK,StatusMessageEnum.UPDATE_SUCCESS.toString());
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.UPDATE_FAIL.toString());
    }

    /*
     * 功能描述〈删除投诉〉
     * @author:yp
     * @param: UserComplaintParam
     * @return:
     * @Date 2021/4/12 15:21
     * @throws
     */
    @GetMapping("/ordinary/deleteUserComplaint")
    @ApiOperation(value = "删除投诉信息",notes = "<span style='color:red;'>删除投诉信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "删除成功"),
            @ApiResponse(code=20001,message = "删除失败")
    })
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "userComplaintId",value = "用户投诉id",dataType = "int",paramType = "query",example = "1"),
    })
    public Result deleteUserComplaint(Integer userComplaintId){
        if(userComplaintId==null){
            return new Result(false,StatusCode.ERROR,"参数错误");
        }
        log.info("开始业务:删除投诉");
        int num = userComplaintService.deleteUserComplaint(userComplaintId);
        log.info("结束业务:删除投诉");
        if (num==1){
            return new Result(true,StatusCode.OK,StatusMessageEnum.UPDATE_SUCCESS.toString());
        }
        return new Result(false,StatusCode.ERROR,StatusMessageEnum.UPDATE_FAIL.toString());
    }


    /*
     * 功能描述〈上传图片〉
     * @author:yp
     * @param:
     * @return:
     * @Date 2021/4/8 17:43
     * @throws
     */
    @PostMapping("/ordinary/addpicture")
    @ApiOperation(value = "上传图片信息",notes = "<span style='color:red;'>上传图片信息的接口</span>")
    @ApiResponses({
            @ApiResponse(code = 20000,message = "上传成功"),
            @ApiResponse(code=20001,message = "上传失败")
    })
    public Result addPicture(@ModelAttribute MultipartFile causeImg) {
        LocalDate localDate = LocalDate.now();
        StringBuffer fileName = new StringBuffer("/");
        fileName.append(localDate.getYear()).append("/")
                .append(localDate.getMonthValue()).append("/")
                .append(localDate.getDayOfMonth()).append("/")
                .append(System.currentTimeMillis());
        String type = causeImg.getOriginalFilename().substring(causeImg.getOriginalFilename().lastIndexOf("."));
        fileName.append(type);
        String url;
        try {
            System.out.println(causeImg.getContentType());
            url = minioTemplate.uploadFile(fileName.toString(), "test", causeImg.getInputStream(), causeImg.getSize(), causeImg.getContentType());
            log.info("上传图片成功");
            return  new Result(true,StatusCode.OK,StatusMessageEnum.SAVE_SUCCESS.toString(),url);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("上传图片失败");
            throw new RuntimeException("上传失败");
        }
    }
}

